import uuid

from drf_haystack.serializers import HaystackSerializer
from rest_framework import serializers

from goods.models import GoodsImage, Goods, GoodsCategory, GoodsNavImage
from goods.search_indexes import GoodsIndex
from user.models import UserProfile
from user.serializers import UserSeriallizer


class GoodsImageSerializers(serializers.ModelSerializer):
    class Meta:
        model = GoodsImage
        fields = ('image',)


class GoodsNavImageSerializers(serializers.ModelSerializer):
    class Meta:
        model = GoodsNavImage
        fields = '__all__'


class GoodsCreateSerializers(serializers.ModelSerializer):
    images = GoodsImageSerializers(many=True)
    sellername = serializers.CharField(source='seller.username')

    class Meta:
        model = Goods
        fields = ['goods_brief', 'goods_price', 'images','sellername']


class GoodsListSerializers(serializers.ModelSerializer):
    class Meta:
        model = Goods
        fields = ['goods_front_image', 'goods_brief', 'goods_price', 'id']
        ordering = '-add_time'


class GoodsDetailSerializers(serializers.ModelSerializer):
    images = GoodsImageSerializers(many=True)
    sellername = serializers.CharField(source='seller.username')
    selleravatar = serializers.ImageField(source='seller.avatar')

    class Meta:
        model = Goods
        fields = ['images', 'goods_sn', 'click_num', 'goods_price', 'goods_brief', 'ship_free', 'is_new', 'sellername',
                  'selleravatar', 'id']


class GoodsIndexSerialiser(HaystackSerializer):
    object = GoodsListSerializers(read_only=True)

    class Meta:
        index_classes = [GoodsIndex]
        fields = ['text', 'object']


class GoodsNavSerialiser(serializers.ModelSerializer):
    name = serializers.CharField(read_only=True)

    # goodscategoryimage = GoodsNavImageSerializers()

    # def get_fields(self):
    #     return GoodsCategory.objects.filter(category_type=1)

    class Meta:
        model = GoodsCategory
        fields = ['name', 'image']
